package com.example.mytestapplication.utils;

import android.text.TextUtils;
import android.util.Log;

import com.example.mytestapplication.TvBoxApi;
import com.facebook.stetho.okhttp3.StethoInterceptor;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by wei on 2017/8/11.
 */

public class LiefengRetrofit {
    private static LiefengRetrofit liefengRetrofit ;
    public static Interceptor mTokenInterceptor = new Interceptor() {
        @Override
        public Response intercept(Chain chain) throws IOException {
            Request originalRequest = chain.request();
            String openId = "default";
//            if (chain.request().url().toString().contains("http://basic"))
//                openId = MyPreferensLoader.getOpenID(MyPreferensLoader.MOUDLES.IHOME, MyPreferensLoader.getUser() != null);
//            if (chain.request().url().toString().contains("http://property")){
//
//                openId = MyPreferensLoader.getOpenID(MyPreferensLoader.MOUDLES.PROPERTY, MyPreferensLoader.getUser() != null);
//                Log.d("openid", "intercept: "+openId);
//                if(TextUtils.isEmpty(openId) || "default".equals(openId) ){
//                    openId = MyPreferensLoader.getOpenID(MyPreferensLoader.MOUDLES.IHOME, MyPreferensLoader.getUser() != null);
//                }
//            }
//
//            if (chain.request().url().toString().contains("http://api.oldpeople")){
//                openId = MyPreferensLoader.getOpenID(MyPreferensLoader.MOUDLES.OLDPEOPLE, MyPreferensLoader.getUser() != null);
//                Log.d("openid", "intercept: "+openId);
//                if(TextUtils.isEmpty(openId) || "default".equals(openId) ){
//                    openId = MyPreferensLoader.getOpenID(MyPreferensLoader.MOUDLES.IHOME, MyPreferensLoader.getUser() != null);
//                }
//            }
            Log.i("", chain.request().url() + "okhttp:openId" + openId);
            Request authorised = originalRequest.newBuilder()
                    .addHeader("openId", openId)
                    .addHeader("Connection", "close")
                    .addHeader("version", "1")
                    .build();
            Log.i("intercept", "openId: " + openId + ",Connection:close" + ",version:" + "1");
            return chain.proceed(authorised);
        }
    };
    private final OkHttpClient client;
    private final TvBoxApi tvBoxApi;

    private LiefengRetrofit(){
        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
        interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        client = new OkHttpClient.Builder()
                //添加拦截器->日志拦截器，输出网络请求和结果的log，level 有：basic/header/body  body应该是全部的意思。
                .addInterceptor(interceptor)
                //失败是否进行重连
                .retryOnConnectionFailure(false)
                //超时时间
                .connectTimeout(15, TimeUnit.SECONDS)
                //读超时时间。
                .readTimeout(25, TimeUnit.SECONDS)
                .addNetworkInterceptor(new StethoInterceptor())
                .addNetworkInterceptor(mTokenInterceptor)
                .build();
        Gson gson = new Gson();

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://basic.test.liefengtech.com/")
                .client(client)
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create(gson))
                .build();

        tvBoxApi = retrofit.create(TvBoxApi.class);
    }

    public static LiefengRetrofit getInstance(){
        if (liefengRetrofit == null) {
            liefengRetrofit = new LiefengRetrofit();
        }

        return liefengRetrofit;
    }

    public TvBoxApi getTvBoxApi(){
        return tvBoxApi;
    }


}
